package mianyang.zhangsan.com.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import mianyang.zhangsan.com.pojo.Clazz;
import mianyang.zhangsan.com.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/clazz")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @RequestMapping("/list")
    @ResponseBody
    public Object list(@RequestParam(value = "page", defaultValue = "1") int page,
                       @RequestParam(value = "rows", defaultValue = "10")int rows,
                       String clazzName){
        Page<Clazz> clazzPage = clazzService.queryByPage(page, rows, clazzName);
        Map<String, Object> map = new HashMap<>();
        map.put("total", clazzPage.getPages());
        map.put("rows", clazzPage.getRecords());
        return map;
    }

    /**
     * 添加班级信息
     * @param clazz
     * @return
     */
    @RequestMapping(value = "/add", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String add(Clazz clazz){
        if (clazz != null){
            clazzService.addClazz(clazz);
            return "{\"success\":true, \"message\":\"添加成功\"}";
        }
        return "{\"success\":false}";
    }

    /**
     * 删除班级信息
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delete(int id){
        if (id != 0){
            clazzService.deleteClazz(id);
            return "{\"success\":true,\"message\":\"删除成功\"}";
        }
        return "{\"success\": false}";
    }

    @RequestMapping(value = "/comboboxList", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Object comboboxList(){
        List<Clazz> clazzList = clazzService.queryInfoAll();
        return clazzList;
    }
}
